package com.loctek.importexport.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loctek.importexport.R;
import com.loctek.importexport.bean.getproductResponse;
import com.loctek.importexport.bean.getstoresiteResponse;
import com.loctek.importexport.mvp.adapter.ProductAdapter;

import java.util.ArrayList;
import java.util.List;


public class UIConfirmOutHousAlertView extends Dialog {

    private Context context;
    private ClickListenerInterface clickListenerInterface;
    private int doLeftTextColor;
    private int doRightTextColor;
    getproductResponse data;
    getstoresiteResponse data1;
    ProductAdapter adapter;
    List<getstoresiteResponse.ProductInfo> list = new ArrayList<>();
    int num;
    String storesite;

    public UIConfirmOutHousAlertView(Context context, getproductResponse data, getstoresiteResponse data1, int num, String storesite) {
        super(context, R.style.UIAlertViewStyle);
        this.context = context;
        this.data = data;
        this.data1 = data1;
        this.num = num;
        this.storesite = storesite;
    }


    public void setdoLeftTextColor(int resId) {
        doLeftTextColor = resId;
    }

    public void setdoRightTextColor(int resId) {
        doRightTextColor = resId;
    }

    public interface ClickListenerInterface {
        public void doLeft();

        public void doRight();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        inite();
    }

    public void inite() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.ui_confirm_out_hous_alert_view, null);
        setContentView(view);

        TextView tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);
        TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);
        TextView tv_productname = (TextView) view.findViewById(R.id.tv_productname);
        TextView tv_scdptypename = (TextView) view.findViewById(R.id.tv_scdptypename);
        TextView tv_num = (TextView) view.findViewById(R.id.tv_num);
        TextView tv_storesite = (TextView) view.findViewById(R.id.tv_storesite);
        LinearLayout empty_ll = (LinearLayout) view.findViewById(R.id.empty_ll);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);

        tv_productname.setText(data.data.name + "");
        tv_scdptypename.setText(data.data.scdptypename + "");
        tv_num.setText(num + "");
        tv_storesite.setText("" + storesite);
        if (data1.data.productList == null || data1.data.productList.size() == 0) {
            empty_ll.setVisibility(View.VISIBLE);
        } else {
            empty_ll.setVisibility(View.GONE);
            list.addAll(data1.data.productList);
        }
        adapter = new ProductAdapter(context, list);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

        if (doLeftTextColor != 0) {
            tvLeft.setTextColor(doLeftTextColor);
        }
        if (doRightTextColor != 0) {
            tvRight.setTextColor(doRightTextColor);
        }

        tvLeft.setOnClickListener(new clickListener());
        tvRight.setOnClickListener(new clickListener());

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();

        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class clickListener implements View.OnClickListener {


        public void onClick(View v) {

            int id = v.getId();
            switch (id) {
                case R.id.tvBtnLeft:
                    clickListenerInterface.doLeft();
                    break;
                case R.id.tvBtnRight:
                    clickListenerInterface.doRight();
                    break;

                default:
                    break;
            }
        }
    }

}
